package com.neu.admin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.neu.common.RequiresPermissionsDesc;
import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.doc.AdminAdController_ResOK;
import com.neu.doc.AdminAdController_list;
import com.neu.doc.AdminCouponController_ResOK;
import com.neu.doc.AdminCouponController_detail;
import com.neu.doc.AdminCouponController_list;
import com.neu.doc.AdminCouponController_userlist;
import com.neu.doc.ResponseOK;
import com.neu.domain.ShoppingAd;
import com.neu.domain.ShoppingCoupon;
import com.neu.domain.ShoppingCouponUser;
import com.neu.service.ShoppingCouponService;
import com.neu.service.ShoppingCouponUserService;
import com.neu.util.CouponConstant;
import com.neu.util.ResponseUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Validated
@RequestMapping("/admin/coupon")
public class AdminCouponController {
    private final Log logger = LogFactory.getLog(AdminCouponController.class);

    @Autowired
    private ShoppingCouponService couponService;
    @Autowired
    private ShoppingCouponUserService couponUserService;

// 测试通过 
    @ApiOperation(value="获取优惠券List)分页数据", notes="分页查询优惠券",response = AdminCouponController_list.class)
    @RequiresPermissions("admin:coupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "查询")
    @GetMapping("/list")
    public Object list() {
    	List<ShoppingCoupon> coupon=couponService.querySelective();
    	return ResponseUtil.okList(coupon);
    }
    
    //ok 通过
    @ApiOperation(value="获取优惠券领取用户信息", notes="分页查询获取优惠券领取用户信息",response = AdminCouponController_userlist.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int"),
        @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true, dataType = "int"),
        @ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "string"),
        @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int"),
        @ApiImplicitParam(name = "size", value = "每页几个", required = false, dataType = "int"),
        @ApiImplicitParam(name = "sort", value = "排序字段", required = true, dataType = "string"),
        @ApiImplicitParam(name = "order", value = "排序规则", required = true, dataType = "string")
})
    @RequiresPermissions("admin:coupon:listuser")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "查询用户")
    @GetMapping("/listuser")
    public Object listuser(Integer userId, Integer couponId, Short status, Integer page, Integer size, String sort, String order) {
    	List<ShoppingCouponUser> couponUser=couponUserService.queryList(userId, couponId, status, page, size, sort, order);
    	return ResponseUtil.okList(couponUser);
    }

    private Object validate(ShoppingCoupon coupon) {
        String name = coupon.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }

    //ok 测试通过
    @ApiOperation(value="添加优惠", notes="添加一条优惠信息", response = AdminCouponController_ResOK.class)
    @RequiresPermissions("admin:coupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true) @RequestBody ShoppingCoupon coupon) {
    	Object error = validate(coupon);
        if (error != null) {
            return error;
        }
        couponService.add(coupon);
        return ResponseUtil.ok(coupon);
    }



    @ApiIgnore//使用该注解忽略这个API
    @RequiresPermissions("admin:coupon:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        ShoppingCoupon coupon = couponService.findById(id);
        return ResponseUtil.ok(coupon);
    }
    
    
    //ok 测试通过
    @ApiOperation(value="编辑优惠", notes="编辑一条优惠信息", response = AdminAdController_ResOK.class)
    @RequiresPermissions("admin:coupon:update")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true) @RequestBody ShoppingCoupon coupon) {
    	Object error = validate(coupon);
        if (error != null) {
            return error;
        }
        couponService.updateById(coupon);
        return ResponseUtil.ok(coupon);
    }
//通过
    @ApiOperation(value="删除优惠", notes="删除一条优惠信息", response = ResponseOK.class)
    @RequiresPermissions("admin:coupon:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)
	@RequestBody ShoppingCoupon coupon) {
    	Integer id = coupon.getId();
    	if (id == null) {
    		return ResponseUtil.badArgument();
    	}
    	couponService.deleteById(id);
    	return ResponseUtil.ok();
}

}
